package com.jinbo.sxzq;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
/************GameDeployView的刷帧线程**************/
public class GameDeployViewDrawThread extends Thread{
    GameDeployView gameDeployView;
    private int sleepSpan = 100;//刷新频率0.1s
    private boolean flag = true;//标志位
    private SurfaceHolder surfaceHolder;
    public GameDeployViewDrawThread(GameDeployView gameDeployView){//构造函数
        this.gameDeployView = gameDeployView;
        surfaceHolder = gameDeployView.getHolder();
    }
    public void  run(){
        Canvas c;
        while (flag){//开始刷新
            c=null;
            try{
                c = this.surfaceHolder.lockCanvas(null);//锁定画布
                synchronized (this.surfaceHolder){//绘制
                    gameDeployView.onDraw(c);
                }
            }finally {
                if(c!=null){
                    this.surfaceHolder.unlockCanvasAndPost(c);//释放画布
                }
            }
            try{//睡眠0.1s
                Thread.sleep(sleepSpan);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    public void setFlag(boolean flag){
        this.flag = flag;
    }
}
